package com.wswy.wzcx.ui.car.result

import android.graphics.*
import android.os.Build
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.SizeUtils
import com.facebook.cache.common.CacheKey
import com.facebook.cache.common.SimpleCacheKey
import com.facebook.common.references.CloseableReference
import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory
import com.facebook.imagepipeline.request.BasePostprocessor
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.R


class CarImagePostprocessor(private val key:String) : BasePostprocessor() {

    companion object {
        private const val TAG = "CarImagePostprocessor"
    }

    override fun process(sourceBitmap: Bitmap?, bitmapFactory: PlatformBitmapFactory?): CloseableReference<Bitmap?>? {
        val w= SizeUtils.dp2px(84f)
        val h = SizeUtils.dp2px(85f)
        val bitmapRef = bitmapFactory?.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        val paint=Paint(Paint.ANTI_ALIAS_FLAG)

        try {

            val srcRect = Rect()
            val destRect = Rect()
            val destRectF = RectF()

            val destBitmap = bitmapRef?.get()
            val canvas = Canvas(destBitmap)

            val imgMW = SizeUtils.dp2px(54f)
            val imgMH = imgMW

            //背景
           val bottom = ImageUtils.getBitmap(R.drawable.query_caravatar_pic,w,h)


            srcRect.set(0,0,bottom.width,bottom.height)
            destRect.set(0,0,w,h)

            destRectF.set(0f,0f,w.toFloat(),h.toFloat())


            //var saveCount = canvas.save()



            val carImage = ImageUtils.compressByScale(sourceBitmap,imgMW,imgMH)

            paint.xfermode = null
            paint.color = Color.WHITE

            //中间的圆

            val left = SizeUtils.dp2px(12f).toFloat()
            val top = SizeUtils.dp2px(5f).toFloat()

            val r = SizeUtils.dp2px(24f).toFloat()

//            canvas.drawCircle(SizeUtils.dp2px(45f).toFloat(),
//                    SizeUtils.dp2px(36f).toFloat(),
//                    SizeUtils.dp2px(30f).toFloat(), paint)


            canvas.drawCircle(left+r,top+r,r,paint)

            paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP)


            //paint.xfermode = null

            //canvas.drawBitmap(carImage,SizeUtils.dp2px(17f).toFloat(),SizeUtils.dp2px(9f).toFloat(),paint)

            canvas.drawBitmap(carImage,left,top,paint)

//            canvas.restoreToCount(saveCount)
//
//            saveCount =  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                canvas.saveLayer(destRectF,null)
//            }else{
//                canvas.saveLayer(destRectF, null, Canvas.ALL_SAVE_FLAG)
//            }
//
//            canvas.drawARGB(0,0,0,0)

            //canvas.drawCircle()
            //canvas.drawRect(0f,0f,40f,50f,paint)
            //canvas.drawBitmap(sourceBitmap)



            paint.xfermode = null

            //canvas.drawRect(60f,30f,100f,100f,paint)

//            canvas.drawCircle(SizeUtils.dp2px(45f).toFloat(),
//                    SizeUtils.dp2px(35f).toFloat(),
//                    SizeUtils.dp2px(29f).toFloat(), paint);

            canvas.drawCircle(left+r,top+r,r,paint)


            paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.XOR)

            canvas.drawBitmap(bottom,srcRect,destRect,paint)

//            canvas.restoreToCount(saveCount)

            return CloseableReference.cloneOrNull(bitmapRef)
        }finally {
            CloseableReference.closeSafely(bitmapRef)
        }
        //return super.process(sourceBitmap, bitmapFactory)
    }




    override fun getPostprocessorCacheKey(): CacheKey? {
        return SimpleCacheKey(
                "----CarImagePostprocessor---$key")
    }
}